package org.daidai.abstrusewriting.homepage.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.live.activitys.LiveDetailActivity;
import org.daidai.abstrusewriting.homepage.live.adapters.MyAdapter;
import org.daidai.abstrusewriting.homepage.live.models.liveItem;
import org.daidai.abstrusewriting.homepage.live.models.liveSmiple;
import org.daidai.abstrusewriting.homepage.live.networks.ClientNetwork;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AnchorFragment extends BaseFragment {

    private List<liveItem> list;

    public AnchorFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveResult(List<liveItem> items)
    {
        if (items != null) {
            if ( items.get(0)instanceof liveItem)
            {
                list = items;
            }
//            Log.d("dddddddddddddddddd", "onReceiveResult: "+list.size());
//            Log.d("dddddddddddddddddd", "onReceiveResult: "+list.get(0).getList().get(0).getNickname());
//            Log.d("dddddddddddddddddd", "onReceiveResult: "+list.get(0).getList().get(1).getNickname());
//            Log.d("dddddddddddddddddd", "onReceiveResult: "+list.get(0).getList().get(2).getNickname());
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
         ClientNetwork.doLiveMain();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_anchor, container, false);
        ClientNetwork.doLiveMain();
        ListView lv= (ListView) ret.findViewById(R.id.live_list);
        MyAdapter myAdapter = new MyAdapter(getContext(), list);
        Log.d("ppppppppppp", "onCreateView: ");
        lv.setAdapter(myAdapter);
        return ret;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnRecevieFromAdapter(liveSmiple simple)
    {
        Intent intent = new Intent(getContext(), LiveDetailActivity.class);
        intent.putExtra("data",simple);
        startActivity(intent);
    }

}
